<?php

/**
 * @version		$Id$
 * @package		Joomla.Administrator
 * @subpackage	JoomDOC_2
 * @author      ARTIO s.r.o., info@artio.net, http:://www.artio.net
 * @copyright	Copyright (C) 2011 Artio s.r.o.. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die();

/**
 * Process custom events after JoomDOC_23 installing through Joomla installer.
 */
function com_install($installation = true)
{
    /* Load Joomla framework */
    $db = JFactory::getDbo();
    /* @var $db JDatabase */
    $document = JFactory::getDocument();
    /* @var $document JDocument */
    $language = JFactory::getLanguage();
    /* @var $language JLanguage */
    $mainframe = JFactory::getApplication();
    /* @var $mainframe JApplication */
    // Add style it this way because in Joomla 1.7.2 doesn't work placing CSS into page head during component installation
    echo '<style type="text/css">';
    echo '<!--';
    echo '#JoomDOC_2Install{padding: 10px;}';
    echo '#JoomDOC_2Install h2{color: #B7CF5F;margin: 0;padding: 15px 0;}';
    echo '#JoomDOC_2Install p{margin: 0;text-align: justify;width: 50%;}';
    echo '#JoomDOC_2Install a{color: #B7CF5F;display: block;font-size: 1.1em;font-weight: bold;padding: 10px 0;}';
    echo '#JoomDOC_2Install img{float: left;margin: 0 10px 0 0;}';
    echo '#JoomDOC_2Install form{padding: 15px 0;}';
    echo '-->';
    echo '</style>';
    // load component language
    $language->load('com_joomdoc_2', JPATH_ADMINISTRATOR);
    // install component extensions
    if ($installation) {
        // register JoomDOC_2 installer to extend Joomla installer
        JLoader::register('AInstaller', JPATH_ROOT . '/administrator/components/com_joomdoc_2/libraries/joomdoc_2/installer/installer.php');
        AInstaller::install(JPATH_ROOT . '/administrator/components/com_joomdoc_2/extensions');
    }
    // current database prefix
    $pr = $mainframe->getCfg('dbprefix');
    // all tables in Joomla database
    $joomla = $db->getTableList();
    // all JoomDOC_22 tables
    $joomdoc_2 = array($pr . 'joomdoc_2', $pr . 'joomdoc_2_groups', $pr . 'joomdoc_2_history', $pr . 'joomdoc_2_licenses', $pr . 'joomdoc_2_log');
    // all JoomDOC_22 tables in backup form
    $joomdoc_2Bckp = array($pr . 'joomdoc_22', $pr . 'joomdoc_22_groups', $pr . 'joomdoc_22_history', $pr . 'joomdoc_22_licenses', $pr . 'joomdoc_22_log');
    // all DocMAN tables
    $docman = array($pr . 'docman', $pr . 'docman_groups', $pr . 'docman_history', $pr . 'docman_licenses', $pr . 'docman_log');
    echo '<div id="JoomDOC_2Install">';
    if ($installation) {
        // introduction about JoomDOC_23
        echo '<h2>' . JText::_('JOOMDOC') . '</h2>';
        echo '<img src="' . JURI::root(true) . '/components/com_joomdoc_2/assets/images/icon-48-joomdoc_2.png" alt="" />';
        echo '<p>' . JText::_('JOOMDOC_DESC') . JText::_('JOOMDOC_INSTALL_INFO') . '</p>';
        echo '<p><a href="' . JRoute::_('index.php?option=com_joomdoc_2') . '" title="">' . JText::_('JOOMDOC_OPEN') . '</a></p>';
    }
    $nothingToMigrate = true;
    if (array_intersect($joomdoc_2, $joomla) == $joomdoc_2 || array_intersect($joomdoc_2Bckp, $joomla) == $joomdoc_2Bckp) {
        // JoomDOC_22 was installed, prompt user to migrate data from JoomDOC_22 into JoomDOC_23
        echo '<h2>' . JText::_('JOOMDOC_JOOMDOC2_FOUND') . '</h2>';
        echo '<img src="' . JURI::root(true) . '/components/com_joomdoc_2/assets/images/icon-48-joomdoc_22.png" alt="" />';
        echo '<p>' . JText::_('JOOMDOC_JOOMDOC2_MIGRATION_INFO') . '</p>';
        echo '<form id="joomdoc_22import" name="joomdoc_22import" method="post" action="index.php?option=com_joomdoc_2&amp;task=migration.run&amp;component=joomdoc_2">';
        echo '<label for="docbase">' . JText::_('JOOMDOC_JOOMDOC2_MIGRATION_LABEL') . '</label>';
        echo '<input type="text" name="docbase" id="docbase" value="" size="50" />';
        echo '<button onclick="if(this.form.docbase.value.trim()==\'\'){alert(\'' . JText::_('JOOMDOC_MIGRATION_ADD_DOCBASE', true) . '\');return false;}else{this.form.submit();}">' . JText::_('JOOMDOC_JOOMDOC2_MIGRATION_BUTTON') . '</button>';
        echo '</form>';
        if ($installation) {
            // rename JoomDOC_22 tables because there is collision with JoomDOC_23 - only during installation
            $db->setQuery('RENAME TABLE #__joomdoc_2 TO #__joomdoc_22');
            $db->query();
            $db->setQuery('RENAME TABLE #__joomdoc_2_groups TO #__joomdoc_22_groups');
            $db->query();
            $db->setQuery('RENAME TABLE #__joomdoc_2_history TO #__joomdoc_22_history');
            $db->query();
            $db->setQuery('RENAME TABLE #__joomdoc_2_licenses TO #__joomdoc_22_licenses');
            $db->query();
            $db->setQuery('RENAME TABLE #__joomdoc_2_log TO #__joomdoc_22_log');
            $db->query();
            // run JoomDOC_23 database installation again because some tables for JoomDOC_23 are missing after renaming
            $queries = JFile::read(JPATH_ROOT . '/administrator/components/com_joomdoc_2/sql/install.mysql.utf8.sql');
            $queries = $db->splitSql($queries);
            foreach ($queries as $query) {
                $db->setQuery($query);
                $db->query();
            }
        }
        $nothingToMigrate = false;
    }
    if (array_intersect($docman, $joomla) == $docman) {
        // DocMAN was installed, prompt user to migrate data from JoomDOC_22 into JoomDOC_23
        echo '<h2>' . JText::_('JOOMDOC_DOCMAN_FOUND') . '</h2>';
        echo '<img src="' . JURI::root(true) . '/components/com_joomdoc_2/assets/images/icon-48-docman.png" alt="" />';
        echo '<p>' . JText::_('JOOMDOC_DOCMAN_MIGRATION_INFO') . '</p>';
        echo '<form id="docmanimport" name="docmanimport" method="post" action="index.php?option=com_joomdoc_2&amp;task=migration.run&amp;component=docman">';
        echo '<label for="docbase2">' . JText::_('JOOMDOC_DOCMAN_MIGRATION_LABEL') . '</label>';
        echo '<input type="text" name="docbase" id="docbase2" value="" size="50" />';
        echo '<button onclick="if(this.form.docbase.value.trim()==\'\'){alert(\'' . JText::_('JOOMDOC_MIGRATION_ADD_DOCBASE', true) . '\');return false;}else{this.form.submit();}">' . JText::_('JOOMDOC_DOCMAN_MIGRATION_BUTTON') . '</button>';
        echo '</form>';
        $nothingToMigrate = false;
    }
    if (!$installation && $nothingToMigrate) {
        $mainframe->enqueueMessage(JText::sprintf('JOOMDOC_MIGRATION_NOTHING_TO_DO', implode(', ', $docman), implode(', ', $joomdoc_2Bckp)), 'notice');
    }
    echo '</div>';
}
?>